package mobi.smarthosts;

import java.io.File;
import java.util.ArrayList;
import java.util.Date;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.location.Location;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.text.Html;
import android.webkit.MimeTypeMap;

public class IntentHelper {

    public static final String MIME_IMAGE = "image/*";
    public static final String MIME_IMAGE_JPEG = "image/jpeg";
    public static final String MIME_VIDEO = "video/*";
    public static final String MIME_AUDIO = "audio/*";
    public static final String MIME_GEO = "geo:";
    public static final String MIME_TEXT = "text/plain";
    public static final String MIME_PDF = "application/pdf";
    public static final String MIME_ALL = "*/*";

    public static Intent getCameraIntent(File imageFile) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
        Log.d("Launch camera for photo: " + imageFile.getAbsolutePath());
        return intent;
    }

    public static Intent getCameraIntent(Uri imageUri) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        return intent;
    }

    private final static String IMAGE_FILE_NAME_FORMAT = "IMG_%s.jpg";
    private final static String IMAGE_FILE_NAME_DATE_FORMAT = "yyyyMMdd_HHmmss";

    public static File getOutputImageFile(){
        File cameraRollDir = new File(Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_DCIM), "Camera");
        if (!cameraRollDir.exists()) cameraRollDir.mkdirs();
        return new File(cameraRollDir, String.format(IMAGE_FILE_NAME_FORMAT,
                Utils.formatDate(new Date(), IMAGE_FILE_NAME_DATE_FORMAT)));
    }

    public static Uri createImageUri(ContentResolver cr, Location location) {
        long nowLong = new Date().getTime();
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.DATE_ADDED, nowLong);
        values.put(MediaStore.Images.Media.DATE_TAKEN, nowLong);
        values.put(MediaStore.Images.Media.DATE_MODIFIED, nowLong);
        values.put(MediaStore.Images.Media.MIME_TYPE, MIME_IMAGE_JPEG);
        values.put(MediaStore.Images.Media.DATA, getOutputImageFile().getAbsolutePath());
        if (location != null ) {
            values.put(MediaStore.Images.Media.LATITUDE, location.getLatitude());
            values.put(MediaStore.Images.Media.LONGITUDE, location.getLongitude());
        }
        return cr.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
    }

    public static void updateImageUri(ContentResolver cr, Uri imageUri, File imageFile) {
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.SIZE, imageFile.length());
        cr.update(imageUri, values, null, null);
    }

    public static Intent getGalleryIntent(Uri uri) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(uri, MIME_IMAGE);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        return intent;
    }

    public static Intent getChooseImageIntent() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT, null);
        intent.setType(MIME_IMAGE);
        return intent;
    }

    public static Intent getEmailIntent(String subject, String body,
            String[] recipient, ArrayList<Uri> attachmentUris) {
        Intent intent = new Intent(
                Utils.isEmptyCollection(attachmentUris)
                    ? Intent.ACTION_SEND
                    : Intent.ACTION_SEND_MULTIPLE);
        intent.setType(MIME_TEXT);
        intent.putExtra(Intent.EXTRA_EMAIL, recipient);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        // TODO add icon to body: <img src="http://dhvlt3l3txk33.cloudfront.net/GPSLog_MailIcon.png" width="20" height="20"/>
        intent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(body));
        if (!Utils.isEmptyCollection(attachmentUris))
            intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, attachmentUris);
        return intent;
    }

    public static Intent getShareIntent(String title, String text,
            String shareChooserTitle) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setType(MIME_TEXT);
        intent.putExtra(Intent.EXTRA_TITLE, title);
        intent.putExtra(Intent.EXTRA_SUBJECT, title);
        intent.putExtra(Intent.EXTRA_TEXT, text);
        return Intent.createChooser(intent, shareChooserTitle);
    }

    public static Intent getGPSSettingIntent() {
        return new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS);
    }

    public static Intent getDefaultViewIntent(File file) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.setDataAndType(Uri.fromFile(file),
                MimeTypeMap.getSingleton().getMimeTypeFromExtension(
                        Utils.getFileExtName(file)));
        return intent;
    }

    public static Intent getBrowserIntent(String url) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setData(Uri.parse(url));
        return intent;
    }

}
